package com.fkdx.common.dto;


import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.springframework.http.HttpStatus;

/**
 * @author wangtengfei
 */
public class ResponseDTO<T> {
    private Integer code;
    private String msg;
    private String localMsg;
    private T data;

    public static <E> ResponseDTO<E> buildSuccessDataResp(Object data) {
        ResponseDTO responseDTO = buildResponse(HttpStatus.OK.value(), (String)null, data);
        return responseDTO;
    }

    public static ResponseDTO<Map<String, Object>> buildSuccessDataResp(String key, Object value) {
        HashMap<String, Object> data = new HashMap(1);
        data.put(key, value);
        return buildResponse(HttpStatus.OK.value(), (String)null, data);
    }

    public static ResponseDTO<Map<String, Object>> buildSuccess(ImmutablePair<String, Object>... values) {
        Map<String, Object> map = new HashMap(values.length);
        ImmutablePair[] var2 = values;
        int var3 = values.length;

        for(int var4 = 0; var4 < var3; ++var4) {
            ImmutablePair<String, Object> value = var2[var4];
            map.put(value.left, value.right);
        }

        return buildSuccessDataResp(map);
    }

    public static <T> ResponseDTO<T> buildSuccess(T data) {
        ResponseDTO responseDTO = buildResponse(HttpStatus.OK.value(), (String)null, data);
        return responseDTO;
    }

    public static <T> ResponseDTO<T> buildSuccess() {
        ResponseDTO responseDTO = buildResponse(HttpStatus.OK.value(), "ok", (Object)null);
        return responseDTO;
    }

    public static <E> ResponseDTO<E> buildErrorResp(String msg) {
        ResponseDTO responseDTO = buildErrorResp(msg, (Object)null);
        return responseDTO;
    }

    public static <E> ResponseDTO<E> buildErrorResp(String msg, E data) {
        ResponseDTO responseDTO = buildResponse(HttpStatus.BAD_REQUEST.value(), msg, data);
        return responseDTO;
    }

    public static <T> ResponseDTO<T> buildError(String msg) {
        ResponseDTO responseDTO = buildError(msg, (Object)null);
        return responseDTO;
    }

    public static <T> ResponseDTO<T> buildSysError() {
        return buildSysError("SYSTEM_EXCEPTION");
    }

    public static <T> ResponseDTO<T> buildParamError() {
        ResponseDTO responseDTO = buildResponse(HttpStatus.BAD_REQUEST.value(), "PARAMETER_MISSING", (Object)null);
        return responseDTO;
    }

    public static <T> ResponseDTO<T> buildParamError(String msg) {
        ResponseDTO responseDTO = buildResponse(HttpStatus.BAD_REQUEST.value(), msg, (Object)null);
        return responseDTO;
    }

    public static <T> ResponseDTO<T> buildSysError(String msg) {
        ResponseDTO responseDTO = buildResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), msg, (Object)null);
        return responseDTO;
    }

    public static <T> ResponseDTO<T> buildNotFound() {
        ResponseDTO responseDTO = buildResponse(HttpStatus.OK.value(), "NOT_FOUND", (Object)null);
        return responseDTO;
    }

    public static <T> ResponseDTO<T> buildForbidden() {
        ResponseDTO responseDTO = buildResponse(HttpStatus.FORBIDDEN.value(), "FORBIDDEN", (Object)null);
        return responseDTO;
    }

    public static <T> ResponseDTO<T> buildError(String msg, T data) {
        ResponseDTO responseDTO = buildResponse(HttpStatus.BAD_REQUEST.value(), msg, data);
        return responseDTO;
    }

    public static <E> ResponseDTO<E> buildResponse(int code, String msg, E data) {
        ResponseDTO responseDTO = new ResponseDTO();
        responseDTO.setCode(code);
        responseDTO.setMsg(msg);
        responseDTO.setData(data);
        return responseDTO;
    }

    @JsonIgnore
    public boolean isSuccess() {
        return null != this.code && this.code.equals(HttpStatus.OK.value());
    }

    @JsonIgnore
    public boolean isFailed() {
        return !this.isSuccess();
    }

    @JsonIgnore
    public boolean hasData() {
        if (this.isFailed()) {
            return false;
        } else {
            return null != this.data;
        }
    }

    public Integer getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.msg;
    }

    public String getLocalMsg() {
        return this.localMsg;
    }

    public T getData() {
        return this.data;
    }

    public void setCode(final Integer code) {
        this.code = code;
    }

    public void setMsg(final String msg) {
        this.msg = msg;
    }

    public void setLocalMsg(final String localMsg) {
        this.localMsg = localMsg;
    }

    public void setData(final T data) {
        this.data = data;
    }



    public ResponseDTO(final Integer code, final String msg, final String localMsg, final T data) {
        this.code = code;
        this.msg = msg;
        this.localMsg = localMsg;
        this.data = data;
    }

    public ResponseDTO() {
    }
}
